package com.example.media.controller;


import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.media.annotation.BloomFilter;
import com.example.media.entity.MacVod;
import com.example.media.service.IMacVodService;
import com.example.media.vo.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author tangxinmao
 * @since 2020-01-10
 */
@RestController
@RequestMapping("macVod")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class MacVodController {
    private final IMacVodService iMacVodService;

    @PostMapping(path = "page")
    @BloomFilter
    public ResponseEntity page(@RequestBody PageInfo<MacVod> pageInfo) {
        IPage<MacVod> iPage = new Page<>(pageInfo.getPage(), pageInfo.getSize(), true);
        pageInfo.getOrderList().forEach(order -> {
            if (order.getDirection() == Sort.Direction.ASC) {
                iPage.orders().add(OrderItem.asc(order.getProperty()));
            }
            if (order.getDirection() == Sort.Direction.DESC) {
                iPage.orders().add(OrderItem.desc(order.getProperty()));
            }
        });
        AbstractWrapper wrapper = iMacVodService.lambdaQuery().like(pageInfo.getData().getVodName() != null, MacVod::getVodName, pageInfo.getData().getVodName())
                .eq(pageInfo.getData().getTypeId() != null, MacVod::getTypeId, pageInfo.getData().getTypeId())
                .eq(pageInfo.getData().getTypeId1() != null, MacVod::getTypeId1, pageInfo.getData().getTypeId1()).getWrapper();
        IPage<MacVod> page = iMacVodService.page(iPage, wrapper);
        return ResponseEntity.ok(page);
    }

    @PostMapping(path = "detail")
    @BloomFilter
    public ResponseEntity detail(@RequestBody MacVod macVod) {
        MacVod macVodDB = iMacVodService.getById(macVod.getVodId());
        return ResponseEntity.ok(macVodDB);
    }

}
